package com.youjia.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.youjia.annotation.RequiredLog;
import com.youjia.config.PayConfig;
import com.youjia.pojo.ProductOrder;
import com.youjia.service.LogService;
import com.youjia.service.ProductOrderService;
import com.youjia.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController //@Controller +  @ResponseBody
@CrossOrigin
@Component
@RequestMapping(method=RequestMethod.POST,produces = "text/html;charset=utf-8")
@ResponseBody
public class Paycontroller{
    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private LogService logService;

    @RequiredLog
    @PostMapping("/order/zf")
    public String zf(@RequestBody ProductOrder productOrder ) throws Exception {
        AlipayClient alipayClient= PayConfig.config();
        AlipayTradePagePayRequest alipayPageRequest =PayConfig.returnUrl();
        //拼接参数
        alipayPageRequest.setBizContent("{\"out_trade_no\":\"" + productOrder.getOrderId() + "\","
                + "\"total_amount\":\"" + productOrder.getPayment() + "\","
                + "\"subject\":\"" + productOrder.getProductName() + "\","
                + "\"body\":\"" +productOrder.getProductName()+ "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        System.out.println(alipayClient);
        System.out.println(alipayPageRequest.getBizContent());
        return alipayClient.pageExecute(alipayPageRequest).getBody();
    }
    @PostMapping("/order/zf2")
    public String zf2(@RequestBody ProductOrder productOrder ) throws Exception {
        AlipayClient alipayClient= PayConfig.config();
        AlipayTradePagePayRequest alipayPageRequest =PayConfig.returnUrl();
        //拼接参数
        alipayPageRequest.setBizContent("{\"out_trade_no\":\"" + productOrder.getOrderId() + "\","
                + "\"total_amount\":\"" + productOrder.getPayment() + "\","
                + "\"subject\":\"" + productOrder.getProductName() + "\","
                + "\"body\":\"" +productOrder.getProductName()+ "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        System.out.println(alipayClient);
        System.out.println(alipayPageRequest.getBizContent());
        return alipayClient.pageExecute(alipayPageRequest).getBody();
    }

   public  void  log (Map<String,Object> map){
       logService.insert(map);
   }

}
